package com.drcoffee.drcoffeemall.mapper;

import com.drcoffee.drcoffeemall.entry.*;
import com.drcoffee.drcoffeemall.entry.search.OrderSearch;
import jdk.jshell.Snippet;
import net.sf.jasperreports.engine.util.StyleResolver;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface OrderMapper {
    List<String> getOrderCode(String search);

    List<Order> getAllOrder(OrderSearch orderSearch);

    List<OrderAttachment> getOrderPart(String code, String language);

    boolean submitOrder(Order order);

    boolean submitOrderAttachment(List<OrderAttachment> orderAttachmentsList);

    Order getOrderAndPart(String order_code, String language);

    List<OrderAttachment> getAllOrderAttachments(@Param("codes") List<String> codes, @Param("language") String language);

    @Select("SELECT * FROM t_order WHERE code = #{order_code}")
    Order getOrderByCode(String orderCode);

    @Update("UPDATE t_order SET status = #{status},${status_time} = CURRENT_TIMESTAMP WHERE code = #{order_code}")
    void setStatus(String order_code, String status, String status_time, String date_time);


    @Select("SELECT order_total FROM t_order WHERE code = #{code}")
    BigDecimal getOrderTotal(String code);

    @Select("SELECT count(0) FROM t_trade where order_code = #{order_code}")
    Integer getOrderTradeByCode(String order_code);

    @Update("UPDATE t_order SET pay_method = #{pay_method} WHERE code = #{code}")
    void setPayMethod(String code, int pay_method);

    @Update("UPDATE t_order SET back_status = #{back_status} WHERE code = #{code}")
    void setBackStatus(String code, int back_status);


    @Insert("INSERT INTO t_pay_voucher(order_code,order_total,proof_path) VALUES ( #{order_code},#{order_total},#{proof_path})")
    void addPayVoucher(PayVoucher payVoucher);

    @Select("SELECT CONCAT(b.o_file_path, b.folder, a.proof_path) as path " +
            "FROM t_pay_voucher a,t_file_path b " +
            "WHERE order_code = #{code} AND b.bsh_one = 'J' AND b.bsh_two = 1")
    String getPayVoucher(String code);

    List<BigDecimal> getOrderAttachmentsByOrder(String code, List<String> partCodes);


    @Select("SELECT trade_platform FROM t_trade WHERE order_code = #{code} AND trade_type = 'I'")
    Integer getPayType(String code);


    @Update("UPDATE t_order SET status = #{status} ${status_time} = CURRENT_TIMESTAMP WHERE code = #{code}")
    void setOrderStatus(String code, Integer status, String status_time);

    @Update("UPDATE t_order SET status = 0 WHERE code = #{code} AND status = 1")
    void cancelOrder(String code);

    @Update("UPDATE t_order SET refund_status = 1 WHERE code = #{code}")
    void setRefundStatus(String code, Integer status);

    @Select("SELECT transport_code FROM t_order WHERE code = #{orderCode}")
    String getTransportCodeByOrderCode(String orderCode);

    @Update("UPDATE t_order SET invoice_status = #{invoice_status} WHERE code = #{order_code} AND invoice_status = 0")
    void setInvoiceStatusByCode(String order_code, Integer invoice_status);

}
